package com.banzhi.shop.activity;

import android.content.Intent;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.banzhi.shop.R;
import com.banzhi.shop.adapter.ChoiceAdapter;
import com.banzhi.shop.base.BaseActivity;
import com.banzhi.shop.data.ChoiceData;
import com.banzhi.shop.fragment.ChoiceFragments;
import com.banzhi.shop.fragment.GroupingSelectFragment;
import com.banzhi.shop.fragment.MyGroupingFragments;
import com.banzhi.shop.model.CallBack.ChoiceCallBack;
import com.banzhi.shop.model.Impl.ChoiceImpl;
import com.banzhi.shop.utils.ASUtils;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.TabLayoutUtil;
import com.banzhi.shop.utils.ToastUtils;
import com.banzhi.shop.utils.Translucent;
import com.banzhi.shop.view.FlyBanner;
import com.banzhi.shop.view.NoScrollViewPager;
import com.banzhi.shop.view.VpSwipeRefreshLayout;

import java.util.ArrayList;
import java.util.List;

public class ChoiceActivity extends BaseActivity implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener, FlyBanner.OnItemClickListener, ChoiceCallBack {

    private ImageView mIvBack;
    private VpSwipeRefreshLayout mRefreshLayout;
    private FlyBanner mBanner;
    private TabLayout mTabsChoice;
    private NoScrollViewPager mVpChoice;

    private List<String> mTitles = new ArrayList<String>();
    private ArrayList<Fragment> mFragments = new ArrayList<Fragment>();
    private ChoiceAdapter mChoiceAdapter;
    private AppBarLayout mAppBarlayoutChoice;
    private ChoiceImpl mModel;
    private int page = 1;
    private List<ChoiceData.AdvsBean> mAdvs;
    private List<ChoiceData.CategoryBean> mCategory;
    //  private List<ChoiceData.CategoryBean> mCategoryBeanList = new ArrayList<>();
    // 必须通过new 对象进行初始化
    private List<String> networkImages = new ArrayList<>();
    private ImageView mIvBackIndroduce;
    private ImageView mIvBackG;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choice);
        Translucent.setTranslucent(this);
        initView();
        //网络请求
        mModel = new ChoiceImpl();
        mModel.getData("", page + "", 20 + "", this);

        initListener();
        mChoiceAdapter = new ChoiceAdapter(getSupportFragmentManager(), mTitles, mFragments);
        mVpChoice.setAdapter(mChoiceAdapter);
        mTabsChoice.setupWithViewPager(mVpChoice);
        mTabsChoice.setTabsFromPagerAdapter(mChoiceAdapter);
        mVpChoice.setCurrentItem(0);
    /*    mTabsChoice.post(new Runnable() {
            @Override
            public void run() {
                TabLayoutUtil.setIndicator(mTabsChoice, 55, 55);
            }
        });*/
        mTabsChoice.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                Intent intent = new Intent();
                intent.setAction("Refresh2");
                sendOrderedBroadcast(intent, null);   //有序广播发送
            }
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }

    private void initView() {
        mIvBack = (ImageView) findViewById(R.id.iv_back_gg);
        mRefreshLayout = (VpSwipeRefreshLayout) findViewById(R.id.swipeRefresh_choice);
        mBanner = (FlyBanner) findViewById(R.id.bannerChoice);
        mTabsChoice = (TabLayout) findViewById(R.id.tabs_choice);
        mVpChoice = (NoScrollViewPager) findViewById(R.id.vp_choice);
        mAppBarlayoutChoice = (AppBarLayout) findViewById(R.id.appBarLayoutChoice);
        mIvBackIndroduce = (ImageView) findViewById(R.id.iv_back_fff);
        mIvBackG = (ImageView) findViewById(R.id.iv_back_gggg);

        mAppBarlayoutChoice.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                if (verticalOffset >= 0) {
                    mRefreshLayout.setEnabled(true);
                } else {
                    mRefreshLayout.setEnabled(false);
                }
            }
        });
        DisplayMetrics dm = getResources().getDisplayMetrics();
        int width = dm.widthPixels;
        int height = width * 380 / 800;
        //  int height = width*216/319;
        LinearLayout.LayoutParams params = new AppBarLayout.LayoutParams(width, height);
        //获取当前控件的布局对象
        //params.height=width;//设置当前控件布局的高度
        mBanner.setLayoutParams(params);//将设置好的布局参数应用到控件
        mTitles.add ("全部");
        mTitles.add("满88选3件");
        mTitles.add("满66选3件");
        mTitles.add("满99选4件");


        mFragments.add(new ChoiceFragments().newInstance("0"));
        mFragments.add(new ChoiceFragments().newInstance("1258"));
        mFragments.add(new ChoiceFragments().newInstance("1257"));
        mFragments.add(new ChoiceFragments().newInstance("1259"));

    }

    private void initListener() {
        mIvBack.setOnClickListener(this);
        mRefreshLayout.setOnRefreshListener(this);
        mBanner.setOnItemClickListener(this);
        mIvBackIndroduce.setOnClickListener(this);
        mIvBackG.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back_gg:
                finish();
                break;
            case R.id.iv_back_fff:
                //购物车：
                startActivity(new Intent(this,ChoiceShoppingCartActivity.class));

                break;
            case R.id.iv_back_gggg:
                //帮助说明
                startActivity(new Intent(this,HelpActivity.class));

                break;
        }

    }

    @Override
    public void onRefresh() {
        mModel.getData("", page + "", 20 + "", this);
    }

    @Override
    public void onItemClick(int position) {

        if ("2".equals(mAdvs.get(position).getAction())) {
            Intent intent = new Intent(ChoiceActivity.this, ChoicesDetailsActivity.class);
            intent.putExtra("id", mAdvs.get(position).getLink());
            startActivity(intent);
        }else {
            Intent intent = new Intent(ChoiceActivity.this, WebViewActivity.class);
            intent.putExtra("data", mAdvs.get(position).getLink());
            startActivity(intent);
        }

    }

    @Override
    public void ongetChoiceDataSuccess(Object object) {
        if (mRefreshLayout.isRefreshing()) {
            mRefreshLayout.setRefreshing(false);
        }
        ChoiceData choiceData = (ChoiceData) object;
        mAdvs = choiceData.getAdvs();
        mCategory = choiceData.getCategory();
        initNetBanner();
    }

    private void initNetBanner() {
        if (mAdvs != null) {
            for (int i = 0; i < mAdvs.size(); i++) {
                networkImages.add(mAdvs.get(i).getThumb());
            }
        }
        if (networkImages.size() > 0) {
            mBanner.setImagesUrl(networkImages);
        }

    }

    @Override
    public void ongetChoiceDataError(Object object) {

    }
}
